<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <input type="text" />
  </body>
  <script>
    var oInput = document.querySelector("input");
    oInput.onkeydown = function (e) {
      console.log("keydown"+e.keyCode); 
      //keyCode用来获取键值（包含功能键） 不区分大小写 都按大写
      //当按下回车键输出表单内的值
      if (e.keyCode == 13) {
        console.log(this.value);
      }
    };

    oInput.onkeypress = function (e) {
      console.log("keypress"+e.keyCode);    
      //keyCode用来获取输入字符的ASCII码值（不包含功能键）区分大小写
    };

    //失焦验证
    oInput.onblur = function (e) {
      //正则验证
      if (!/^[A-Za-z0-9_\-]{4,6}$/.test(this.value)) {
        //输入不合法时
        // alert("您输入的内容格式不正确");
      }
    };
  </script>
</html>
